package b;

import t1.IAdd;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class LiveLoadJar implements Runnable {

    public void run() {
        try {
            while (true) {
                URLClassLoader classloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
                String url = "file:/F:/workspace/ideawp/hotLoadClassDemon/target/hotLoadClass-1.0-SNAPSHOT.jar";
                URL classUrl = new URL(url);

                Method addURL = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
                addURL.setAccessible(true);
                addURL.invoke(classloader, new Object[] { classUrl });

                //String className = "t1.Add";
                String className = "t1.Add";
                Class<?> c = Class.forName(className);
                System.out.println("loadclass :"+c);
                IAdd add = (IAdd) c.newInstance();
                System.out.println(add.add(1,2));

                Thread.sleep(2000);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
